/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs;

import org.rails.extjs.AbsBase;
import org.rails.extjs.ajax.IWait;
import org.rails.extjs.data.Store;
import org.rails.extjs.javascript.JavaScript;

public class LoadMask extends AbsBase implements IWait {
	
	private String id;
	private String target;

	public LoadMask(String msg) {
		super();
		setId(createId());
		setMsg(msg);
	}
	
	public LoadMask(String target,String msg) {
		super();
		setId(createId());
		setTarget(target);
		setMsg(msg);
	}
	
	public void setTarget(String target){
		this.target = target;
	}
	
	public String getTarget(){
		return target;
	}

	public void setMsg(String msg){
		addOption("msg",msg);
	}
	
	public String getMsg(){
		return getString("msg","");
	}
	
	public void setMsgCls(String msgCls){
		addOption("msgCls",msgCls);
	}
	
	public String getMsgCls(){
		return getString("msgCls","x-mask-loading");
	}
	
	public void setRemoveMask(Boolean removeMask){
		addOption("removeMask",removeMask);
	}
	
	public Boolean isRemoveMask(){
		return getBoolean("removeMask",false);
	}
	
	public void setStore(Store store){
		addOption("store",store);
	}
	
	public Store getStore(){
		return (Store)getOption("store");
	}
	
	public void setDisabled(Boolean disabled){
		addOption("disabled",disabled);
	}
	
	public Boolean isDisabled(){
		return getBoolean("disabled",false);
	}
	
	public void setId(String id){
		this.id = id;
	}
	
	public String getId(){
		return id;
	}
	
//	@Override
	public JavaScript hide() {
		return new JavaScript("{0}.hide();", getId());
	}
	
//	@Override
	public JavaScript show() {
		return new JavaScript("{0}.show();",id);
	}
	
//	@Override
	public JavaScript error() {
		return new JavaScript("alert('Status: ' + res.status + ' ' + res.statusText);").append(hide());
	}
	
//	@Override
	public String getScript() {
		script = new StringBuffer("");
		script.append(format("var {0}=new Ext.LoadMask(",id));
		
		if(target == null)
			script.append("Ext.getBody()");
		else
			script.append(format("'{0}'",target));
		
		script.append(",");
		script.append(option.createJson());
		script.append(");");
		return script.toString();
	}
	
//	@Override
	public JavaScript getWaitScript(){
		return new JavaScript(getScript());
	}

//	@Override
	public void setLoadText(String loadText) {
		setMsg(loadText);
		
	}

//	@Override
	public String getLoadText() {
		return getMsg();
	}

}
